package com.aerozhonghuan.logisticsmall.modules.mine.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import com.aerozhonghuan.logisticsmall.R;
import com.aerozhonghuan.logisticsmall.framework.base.TitlebarFragment;
import com.aerozhonghuan.logisticsmall.modules.mine.logic.MineWebRequest;
import com.aerozhonghuan.logisticsmall.widget.ProgressDialogIndicator;
import com.aerozhonghuan.oknet2.CommonCallback;
import com.aerozhonghuan.oknet2.CommonMessage;

/**
 * 描述 意见反馈页面
 *
 * @author zhangyonghui
 * 创建日期 2019-05-23
 */
public class FeedBackFragment extends TitlebarFragment {

    private View rootView;
    private ProgressDialogIndicator progressDialogIndicator;
    private EditText etContent;
    private EditText etTitle;
    private Button btnConfirm;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (rootView == null) {
            rootView = inflater.inflate(R.layout.fragment_me_feedback, null);
            initView();
        }
        return rootView;
    }

    private void initView() {
        progressDialogIndicator = new ProgressDialogIndicator(getContext());
        etContent = (EditText) rootView.findViewById(R.id.et_content);
        etTitle = (EditText) rootView.findViewById(R.id.et_title);
        btnConfirm = (Button) rootView.findViewById(R.id.btn_confirm);
        btnConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String title = etTitle.getText().toString().trim();
                String content = etContent.getText().toString().trim();
                if (TextUtils.isEmpty(title)) {
                    alert("请输入反馈标题");
                    return;
                }
                if (TextUtils.isEmpty(content)) {
                    alert("请输入反馈内容");
                    return;
                }
                confirmFeedBack(title, content);
            }
        });
    }

    private void confirmFeedBack(String title, String content) {
        btnConfirm.setClickable(false);
        MineWebRequest.confirmFeedback(getContext(), title, content, progressDialogIndicator, new CommonCallback<String>() {
            @Override
            public void onSuccess(String messsageBodyObject, CommonMessage commonMessage, String allResponseString) {
                alert("反馈成功");
                getActivity().finish();
            }

            @Override
            public boolean onFailure(int httpCode, Exception exception, CommonMessage responseMessage, String allResponseString) {
                btnConfirm.setClickable(true);
                return super.onFailure(httpCode, exception, responseMessage, allResponseString);
            }
        });
    }
}
